package com.wmlt.api;

import com.alibaba.fastjson.JSON;
import com.wmlt.entity.FileResponseData;
import com.wmlt.util.FastdfsUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
 * @description: 文件操作api
 * @param:  
 * @return:  
 * @author liyuxiang
 * @date: 2021/3/24 6:04 下午
 */ 
public class FileApi extends HttpServlet {
    int success;
    String message;
    String fileUrl;
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path=request.getServletPath();
        if ("/file/uploadFile".equals(path)){
            uploadFile(request,response);
        }else if("/file/upload".equals(path)){
            downloadFile(request,response);
        }else if("/file/getAllFiles".equals(path)){

        }
    }
    /** 
     * @description: 上传文件
     * @param:  
     * @return:  
     * @author liyuxiang
     * @date: 2021/3/24 6:05 下午
     */ 
    protected void uploadFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json;charset=utf-8");
        String fileUrl = "";
        try {

            String callback = req.getParameter("callback");
            //使用Apache文件上传组件处理文件上传步骤：
            //1、创建一个DiskFileItemFactory工厂
            DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
            //2、创建一个文件上传解析器
            ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);
            //解决上传文件名的中文乱码
            fileUpload.setHeaderEncoding("UTF-8");
            //3、判断提交上来的数据是否是上传表单的数据
            if(!ServletFileUpload.isMultipartContent(req)){
                //按照传统方式获取数据
                return;
            }
            //4、使用ServletFileUpload解析器解析上传数据，解析结果返回的是一个List<FileItem>集合，每一个FileItem对应一个Form表单的输入项
            List<FileItem> list = fileUpload.parseRequest(req);
            for (FileItem item : list) {
                String uploadFileName = item.getFieldName();

                if("file".equals(uploadFileName)||"editormd-image-file".equals(uploadFileName)){
                    String fileName = item.getName();
                    InputStream is = item.getInputStream();
                    //存放
                    String[] strings = FastdfsUtil.uploadFile(is, fileName);
                    String path = "";
                    for (String value : strings){
                        path+="/"+value;
                    }
                    is.close();
                    success=1;
                    message="上传成功";
                    fileUrl="http://43.128.27.123:12100"+path;
                    System.out.println(fileUrl);
                }


            }

        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            success=0;
            message="上传失败";
            fileUrl="";

        }
        FileResponseData fileResponseData = new FileResponseData(success,message,fileUrl);
        String dataStr = JSON.toJSONString(fileResponseData);
        resp.getWriter().println(dataStr);
    }




    /**
     * @description: 下载文件
     * @param:
     * @return:
     * @author liyuxiang
     * @date: 2021/3/24 6:08 下午
     */
    protected void downloadFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    /**
     * @description: 获取所有文件
     * @param:
     * @return:
     * @author liyuxiang
     * @date: 2021/3/24 6:10 下午
     */
    protected void getAllFiles(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

}
